package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.AccraditationRecord;
import com.lekang.service.IAccraditationRecordService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 审批记录Controller
 * 
 * @author wjy
 * @date 2025-10-17
 */
@RestController
@RequestMapping("/accraditationrecord/accraditationrecord")
public class AccraditationRecordController extends BaseController
{
    @Autowired
    private IAccraditationRecordService accraditationRecordService;

    /**
     * 查询审批记录列表
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccraditationRecord accraditationRecord)
    {
        startPage();
        List<AccraditationRecord> list = accraditationRecordService.selectAccraditationRecordList(accraditationRecord);
        return getDataTable(list);
    }
    /**
     * 查询审批记录列表
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:list')")
    @GetMapping("/list1")
    public TableDataInfo list1(AccraditationRecord accraditationRecord)
    {
        startPage();
        List<AccraditationRecord> list = accraditationRecordService.selectAccraditationRecordList1(accraditationRecord);
        return getDataTable(list);
    }
    /**
     * 根据步骤和外键id查询唯一审批记录
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:query')")
    @GetMapping("/unique")
    public AjaxResult getUniqueRecord(
            @RequestParam("stepNo") Integer stepNo,       // 步骤编号（必传，与实体类字段一致）
            @RequestParam("checkContentId") Long checkContentId // 外键ID（必传，关联的业务ID）
    ) {
        // 调用Service时使用正确的参数名（与方法参数一致）
        AccraditationRecord record = accraditationRecordService.selectByStepNoAndCheckContentId(stepNo, checkContentId);
        return success(record); // 直接返回单条记录（无记录则返回null）
    }
    /**
     * 导出审批记录列表
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:export')")
    @Log(title = "审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccraditationRecord accraditationRecord)
    {
        List<AccraditationRecord> list = accraditationRecordService.selectAccraditationRecordList(accraditationRecord);
        ExcelUtil<AccraditationRecord> util = new ExcelUtil<AccraditationRecord>(AccraditationRecord.class);
        util.exportExcel(response, list, "审批记录数据");
    }

    /**
     * 获取审批记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(accraditationRecordService.selectAccraditationRecordById(id));
    }

    /**
     * 新增审批记录
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:add')")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccraditationRecord accraditationRecord)
    {
        return toAjax(accraditationRecordService.insertAccraditationRecord(accraditationRecord));
    }

    /**
     * 修改审批记录
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:edit')")
    @Log(title = "审批记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccraditationRecord accraditationRecord)
    {
        return toAjax(accraditationRecordService.updateAccraditationRecord(accraditationRecord));
    }

    /**
     * 删除审批记录
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:remove')")
    @Log(title = "审批记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(accraditationRecordService.deleteAccraditationRecordByIds(ids));
    }

     /**
     * 根据drop_content_id查询整条审批记录
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:query')")
    @GetMapping("/dropContentId/{dropContentId}")
    public AjaxResult getByDropContentId(@PathVariable("dropContentId") String dropContentId)
    {
        return success(accraditationRecordService.selectByDropContentId(dropContentId));
    }

     /**
     * 删除审批记录
     * 注：如果记录不存在，也视为删除成功（目标状态已达成）
     */
    @PreAuthorize("@ss.hasPermi('accraditationrecord:accraditationrecord:remove')")
    @Log(title = "审批记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/checkContentId/{checkContentId}")
    public AjaxResult removeByCheckContentId(@PathVariable Long checkContentId)
    {
        int rows = accraditationRecordService.deleteAccraditationRecordByCheckContentId(checkContentId);
        // 即使删除0行（记录不存在），也返回成功，因为目标状态（记录不存在）已达成
        return AjaxResult.success("操作成功");
    }
}
